﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.CMS.ContentTypes;
using Marbles.CMS.ContentTypes.Tasks;

namespace Marbles.CMS.Administrator.Content
{
    public partial class Task : ContentEditorBase
    {

        public string SubAction
        {
            get
            {
                string param = Request.Params["subaction"];
                if (string.IsNullOrEmpty(param)) { return string.Empty; }
                return param.ToLower();
            }
        }

        public int TaskId
        {
            get
            {
                string param = Request.Params["taskid"];
                if (string.IsNullOrEmpty(param)) { return 0; }
                return Convert.ToInt32(param);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                this.Title = "Create Task";
                InitialiseForm();
                if (this.TaskId > 0)
                {
                    ContentManager mgr = new ContentManager();

                    switch (SubAction)
                    {
                        case "cancel":
                            mgr.CancelTask(this.TaskId, Page.User.Identity.Name);
                            Response.Redirect("Default.aspx", true);
                            break;
                        case "complete":
                            mgr.CompleteTask(this.TaskId, Page.User.Identity.Name);
                            Response.Redirect("Default.aspx", true);
                            break;
                        default:
                            LoadItem();
                            break;
                    }
                    this.Title = "Edit Task";
                }
            }
        }

        public void LoadItem()
        {
            ContentManager mgr = new ContentManager();
            TaskEntryItem Item = mgr.GetTaskEntry(this.TaskId, Page.User.Identity.Name);
            txtDueDate.Text = Item.DueDate.ToString("dd/MM/yyyy");
            txtStartDate.Text = Item.StartDate.ToString("dd/MM/yyyy");
            ctlExtDueDate.SelectedDate = Item.DueDate;
            ctlExtStartDate.SelectedDate = Item.StartDate;
            txtNotes.Text = Item.Notes;
            txtSubject.Text = Item.Subject;
            drpPriority.SelectedValue = Item.TaskPriorityId.ToString();
            drpStatus.SelectedValue = Item.TaskStatusId.ToString();
        }

        public void SaveItem()
        {
            DateTime value = DateTime.MinValue;
            ContentManager mgr = new ContentManager();
            TaskEntryItem item = null;
            if (this.TaskId >= 0)
            {
                item = mgr.GetTaskEntry(item.TaskId, Page.User.Identity.Name);
            }
            else
            {
                item = new TaskEntryItem(this.TaskId, Page.User.Identity.Name);
            }

            item.TaskPriorityId = Convert.ToInt32(drpPriority.SelectedValue);
            item.TaskStatusId = Convert.ToInt32(drpStatus.SelectedValue);
            item.Subject = txtSubject.Text;
            if (!DateTime.TryParse(txtDueDate.Text, out value)) { value = DateTime.MaxValue; }
            item.DueDate = value;
            if (!DateTime.TryParse(txtStartDate.Text, out value)) { value = DateTime.MaxValue; }
            item.StartDate = value;
            item.Notes = txtNotes.Text;
            mgr.SaveTask(item);
        }

        private void InitialiseForm()
        {
            ContentManager mgr = new ContentManager();
            drpStatus.DataSource = mgr.GetTaskStates();
            drpPriority.DataSource = mgr.GetTaskPriorities();
            drpStatus.DataBind();
            drpPriority.DataBind();
            ClearForm();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx", true);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            SaveItem();
            Response.Redirect("Default.aspx", true);
        }

        public void ClearForm()
        {
            txtDueDate.Text = DateTime.Now.AddDays(7).ToString("dd/MM/yyyy");
            ctlExtDueDate.SelectedDate = DateTime.Now.AddDays(7);
            ctlExtStartDate.SelectedDate = DateTime.Now;
            txtStartDate.Text = DateTime.Now.ToString("dd/MM/yyyy");
            txtSubject.Text = string.Empty;
            txtNotes.Text = string.Empty;
            drpPriority.ClearSelection();
            drpStatus.ClearSelection();
            drpPriority.SelectedValue = "1";
            if (this.TaskId == 0) { btnSave.Text = "Add"; }
        }
    }
}